package com.yuandian.management.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuandian.api.business_monitor.po.SearchParams;
import com.yuandian.api.management.entity.ConfEmployee;
import com.yuandian.api.management.validate.InsertFlag;
import com.yuandian.api.management.validate.UpdateFlag;
import com.yuandian.bpm.common.core.util.R;
import com.yuandian.bpm.common.log.annotation.SysLog;
import com.yuandian.management.service.ConfEmployeeService;
import com.yuandian.management.vo.EmployeeVo;
import com.yuandian.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
 * 营业员配置表-Controller
 * @author zfsn
 * @date 2023-3-28
 */
@Api(tags = "外部指标管理-导入导出管理-营业员信息")
@RestController
@RequestMapping("/employee")
public class ConfEmployeeController {

	@Autowired
	private ConfEmployeeService confEmployeeService;

	/**
	 * 通过主键查询数据
	 * @param idList 主键集合
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "通过主键查询数据", notes = "通过主键查询数据，支持批量查询")
	@GetMapping("/{ids}")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "ids", value = "id(支持多选)", paramType = "query", required = true, dataTypeClass = Integer.class)
	})
	public R<List<ConfEmployee>> queryByIds(@PathVariable("ids") List<Integer> idList) {
		return R.ok(confEmployeeService.listByIds(idList));
	}

	/**
	 * 分页查询
	 * @param confEmployee 筛选条件
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@PostMapping
	@ApiImplicitParams({
			@ApiImplicitParam(name = "businessHallNum", value = "所属营业厅编号", paramType = "body", required = false, dataType = "String"),
			@ApiImplicitParam(name = "num", value = "营业员编号", paramType = "body", required = false, dataType = "String"),
			@ApiImplicitParam(name = "name", value = "营业员姓名", paramType = "body", required = false, dataType = "String"),
			@ApiImplicitParam(name = "pageSize", value = "每页大小", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "pageNum", value = "当前页数", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "orderByColumn", value = "排序列", paramType = "body", required = true, dataType = "String"),
			@ApiImplicitParam(name = "isAsc", value = "排序的方向值只能是 desc 或者 asc", paramType = "body", required = true, dataType = "String")
	})
	public R<Page<ConfEmployee>> listPage(@RequestBody ConfEmployee confEmployee) {
		return R.ok(confEmployeeService.listPage(confEmployee));
	}

	/**
	 * 新增数据
	 * @param confEmployee 实例对象
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "新增数据", notes = "新增数据")
	@PostMapping("/save")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "businessHallNum", value = "所属营业厅编号", paramType = "body", required = false, dataType = "String"),
			@ApiImplicitParam(name = "num", value = "营业员编号", paramType = "body", required = false, dataType = "String"),
			@ApiImplicitParam(name = "name", value = "营业员姓名", paramType = "body", required = false, dataType = "String"),
	})
	public R<String> save(@RequestBody @Validated(InsertFlag.class) ConfEmployee confEmployee) {
		return confEmployeeService.saveEmployee(confEmployee);
	}

	/**
	 * 通过主键更新数据
	 * @param confEmployee 实例对象
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "通过主键更新数据", notes = "通过主键更新数据")
	@SysLog("更新营业员配置表数据")
	@PutMapping
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "id", paramType = "body", required = false, dataType = "Integer"),
			@ApiImplicitParam(name = "businessHallNum", value = "所属营业厅编号", paramType = "body", required = false, dataType = "String"),
			@ApiImplicitParam(name = "num", value = "营业员编号", paramType = "body", required = false, dataType = "String"),
			@ApiImplicitParam(name = "name", value = "营业员姓名", paramType = "body", required = false, dataType = "String"),
	})
	public R<String> updateById(@RequestBody @Validated(UpdateFlag.class) ConfEmployee confEmployee) {
		return confEmployeeService.updateEmployeeById(confEmployee);
	}

	/**
	 * 通过主键删除数据
	 * @param ids 主键
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "通过主键删除数据", notes = "通过主键删除数据")
	@SysLog("删除营业员配置表数据")
	@DeleteMapping("/delete/{ids}")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "ids", value = "id(支持多选)", paramType = "query", required = true, dataType = "Integer")
	})
	public R<Boolean> removeByIds(@PathVariable("ids") List<Integer>  ids){
		return R.ok(confEmployeeService.removeByIds(ids));
	}

	/**
	 * 查询营业员信息并装换成 Map<操作员ID， 操作员名称>
	 *
	 * @param params 业务编码集合
	 * @return 搜索选项vo对象集合
	 */
	@ApiIgnore
	@PostMapping("/getOperatorMap")
	public R<Map<String, String>> getOperatorMap(@RequestBody SearchParams params) {
		return R.ok(confEmployeeService.getOperatorMap(params));
	}

	/**
	 * 根据操作员列表获取操作员Id列表
	 *
	 * @param operatorName
	 * @return 搜索选项vo对象集合
	 */
	@ApiIgnore
	@PostMapping("/{operatorName}")
	public R<List<String>> getOperatorIdListByOperatorNameList(@PathVariable("operatorName") String operatorName){
		return R.ok(confEmployeeService.getOperatorIdListByOperatorNameList(operatorName));
	}

	/**
	 * 营业员信息模板下载
	 *
	 * @param response response
	 * @throws IOException
	 */
	@ApiOperation(value = "营业员信息模板下载", notes = "营业员信息模板下载")
	@GetMapping("/downloadTemplate")
	public void getTemplate(HttpServletResponse response) throws IOException {
		ExcelUtil.writeExcel(EmployeeVo.class, "营业员信息模板", "营业员信息模板", new ArrayList<>(), response);
	}

	/**
	 * 营业员信息上传
	 *
	 * @param file file
	 * @return Result
	 * @throws IOException
	 */
	@PostMapping("/upload")
	@ApiOperation(value = "营业员信息上传", notes = "营业员信息上传")
	public R upload(@RequestParam("file") MultipartFile file) throws IOException {
		Map<String, List<EmployeeVo>> resultMap = ExcelUtil.readAndSaveExcelData(file, 0,2, EmployeeVo.class, confEmployeeService);

		List<EmployeeVo> successList = resultMap.get("result");
		List<EmployeeVo> failList = resultMap.get("fail");
		if (!CollectionUtils.isEmpty(successList) && CollectionUtils.isEmpty(failList)) {
			return R.ok("导入完成，成功条数：" + successList.size() + ",失败条数：" + failList.size());
		}

		return R.failed("导入完成，成功条数：" + successList.size() + ",失败条数：" + failList.size());
	}

}
